﻿using MahApps.Metro.Controls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// Logo控件
    /// 和Image控件一样可以拉伸
    /// </summary>
    public class LevsLogo : ContentControl
    {
        private readonly Size _naturalSize = new Size(460, 147);
        public static readonly DependencyProperty StretchProperty =
            Viewbox.StretchProperty.AddOwner(typeof(LevsLogo));

        public Stretch Stretch
        {
            get => (Stretch)GetValue(StretchProperty);
            set => SetValue(StretchProperty, value);
        }

        public LevsLogo()
        {
            this.Style = (Style)FindResource("LevsLogo");
            this.Height = double.NaN;
            this.Width = double.NaN;
            IsTabStop = false;
            Focusable = false;
            FocusVisualStyle = null;
        }

        /// <inheritdoc />
        protected override Size MeasureOverride(Size constraint)
        {
            base.MeasureOverride(constraint);
            return MeasureArrangeHelper(constraint);
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            base.ArrangeOverride(arrangeSize);
            return MeasureArrangeHelper(arrangeSize);
        }

        private Size MeasureArrangeHelper(Size inputSize)
        {
            var naturalSize = _naturalSize;

            Size scaleFactor = ComputeScaleFactor(inputSize,
                naturalSize,
                this.Stretch,
                StretchDirection.Both);

            return new Size(naturalSize.Width * scaleFactor.Width, naturalSize.Height * scaleFactor.Height);
        }

        private static Size ComputeScaleFactor(Size availableSize,
                                                Size contentSize,
                                                Stretch stretch,
                                                StretchDirection stretchDirection)
        {
            double scaleX = 1.0;
            double scaleY = 1.0;

            bool isConstrainedWidth = !double.IsPositiveInfinity(availableSize.Width);
            bool isConstrainedHeight = !double.IsPositiveInfinity(availableSize.Height);

            if ((stretch == Stretch.Uniform || stretch == Stretch.UniformToFill || stretch == Stretch.Fill)
                 && (isConstrainedWidth || isConstrainedHeight))
            {
                scaleX = contentSize.Width.IsCloseTo(0) ? 0.0 : availableSize.Width / contentSize.Width;
                scaleY = contentSize.Height.IsCloseTo(0) ? 0.0 : availableSize.Height / contentSize.Height;

                if (!isConstrainedWidth) scaleX = scaleY;
                else if (!isConstrainedHeight) scaleY = scaleX;
                else
                {
                    switch (stretch)
                    {
                        case Stretch.Uniform:
                            double minScale = scaleX < scaleY ? scaleX : scaleY;
                            scaleX = scaleY = minScale;
                            break;

                        case Stretch.UniformToFill:
                            double maxScale = scaleX > scaleY ? scaleX : scaleY;
                            scaleX = scaleY = maxScale;
                            break;

                        case Stretch.Fill:
                            break;
                    }
                }

                switch (stretchDirection)
                {
                    case StretchDirection.UpOnly:
                        if (scaleX < 1.0) scaleX = 1.0;
                        if (scaleY < 1.0) scaleY = 1.0;
                        break;

                    case StretchDirection.DownOnly:
                        if (scaleX > 1.0) scaleX = 1.0;
                        if (scaleY > 1.0) scaleY = 1.0;
                        break;

                    case StretchDirection.Both:
                        break;

                    default:
                        break;
                }
            }

            return new Size(scaleX, scaleY);
        }
    }
}